package vn.com.vhc.babyui;

import vn.com.vhc.json.UserFunctions;
import vn.com.vhc.library.Alert;
import vn.com.vhc.library.Progress;
import vn.com.vhc.library.SimpleCrypto;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Baby extends Activity {

	EditText edtUserName;
	EditText edtPassword;
	EditText edtConfirm;
	EditText edtEmail;
	Button btnRegister;
	Button btnLogin;

	String strUserName, strPassword, strEmail, strConfirm, strFamily = "";
	private Alert alert;
	private SimpleCrypto sc = new SimpleCrypto();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baby);

		// Importing all assets like buttons, text fields
		edtUserName = (EditText) findViewById(R.id.txtUsername);
		edtPassword = (EditText) findViewById(R.id.txtPassword);
		edtConfirm = (EditText) findViewById(R.id.txtConfirm);
		edtEmail = (EditText) findViewById(R.id.txtEmail);
		btnRegister = (Button) findViewById(R.id.btnRegister);
		btnLogin = (Button) findViewById(R.id.btnLogin);

	}

	@Override
	protected void onStart() {
		super.onStart();
		// Login button Click Event

		btnLogin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(getApplicationContext(), Login.class);
				startActivity(i);

			}
		});
		RadioGroup rd = (RadioGroup) findViewById(R.id.rdFamily);
		rd.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rdButtonY:
					strFamily = "Y";
					break;
				case R.id.rdButtonN:
					strFamily = "N";
					break;
				default:
					strFamily = "Y";
					break;
				}

			}
		});
		btnRegister.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				strUserName = edtUserName.getText().toString();
				strPassword = edtPassword.getText().toString();
				strEmail = edtEmail.getText().toString();
				strConfirm = edtConfirm.getText().toString();

				try {
					if (TestInfo(strUserName, strPassword, strEmail, strConfirm) == true) {
						strPassword = sc.encrypt("key", strPassword);
						String strResult = new UserFunctions().registerUser(
								strUserName, strPassword, strEmail, strFamily);
						Intent i = new Intent(getApplicationContext(),
								DialogActivity.class);
						if (strResult.contains("true")) {
							
							i.putExtra("INFO", "SUCCESS");
							i.putExtra("TAG", "REGISTER");
							
						}

						else {
							
							i.putExtra("TAG", "REGISTER");
							i.putExtra("INFO", "FAIL");
							
						}
						startActivity(i);
					
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
	}

	private boolean TestInfo(String pUserName, String pPass, String pEmail,
			String pConfirm) {
		alert = new Alert();
		if (pUserName.equalsIgnoreCase("") || pPass.equalsIgnoreCase("")
				|| pEmail.equalsIgnoreCase("") || pConfirm.equalsIgnoreCase("")) {

			alert.AlertInfo(Baby.this, "Lỗi", "Bạn nhập thiếu thông tin");
			return false;
		} else {
			if (!pConfirm.equalsIgnoreCase(pPass)) {
				alert.AlertInfo(Baby.this, "Lỗi", "Mật khẩu không trùng khớp");
				return false;
			}
			if (pEmail.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")||pEmail.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+.[a-z]+")) {
				
				return true;
			}
			else
			{
				alert.AlertInfo(Baby.this, "Lỗi", "Bạn nhập sai email");
				return false;
			}
				

			
		}

	}

	
}
